@extends('admin.index.layout')

@section('title', $title)

@section('content')
    
        <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                用户管理
                <small>用户列表</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> 主页</a></li>
                <li><a href="#">用户管理</a></li>
                <li class="active">用户列表</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">


                    <div class="box">
                        <div class="box-header">
                            <h3 class="box-title">查看用户列表</h3>
                        </div><!-- /.box-header -->
                        <div class="box-body">
                            <form action="{{ url('/admin/user/index') }}">
                                <div class="row">
                                    <div class="col-md-4">
                                        <div class="input-group margin">
                                            <input type="text" class="form-control" value="{{ $request['keywords'] or '' }}" name="keywords">
                                            <span class="input-group-btn">
                                              <button class="btn btn-info btn-flat" type="submit">搜索</button>
                                            </span>
                                        </div>
                                    </div>
                                    <div class="col-md-2  col-md-offset-6">
                                        <div class="form-group">
                                        
                                            <label>Select</label>
                                            <select name="num" class="form-control">
                                                <option
                                                @if(!empty($request['num']) 
                                                && $request['num'] == 10)
                                                    selected = "selected"
                                                @endif
                                                >10</option>
                                                <option
                                                @if(!empty($request['num']) 
                                                && $request['num'] == 25)
                                                    selected = "selected"
                                                @endif
                                                >25</option>
                                                <option
                                                @if(!empty($request['num']) 
                                                && $request['num'] == 50)
                                                    selected = "selected"
                                                @endif
                                                >50</option>
                                                <option
                                                @if(!empty($request['num']) 
                                                && $request['num'] == 100)
                                                    selected = "selected"
                                                @endif
                                                >100</option>
                                            </select>

                                        </div>
                                    </div>
                                </div>
                            </form>
                                
                            <table id="example1" class="table table-bordered table-striped">
                                <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>用户名</th>
                                    <th>邮箱</th>
                                    <th>头像</th>
                                    <th>权限</th>
                                    <th>状态</th>
                                    <th>编辑</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($data as $u)
                                <tr>
                                    <td class="ids">{{$u -> id }}</td>
                                    <td class="name">{{$u -> name }}</td>
                                    <td>{{$u -> email }}</td>
                                    <td><img src="/uploads/yhtx/{{$u -> pic }}" style="width:50px" /></td>
                                    <td class="auth">
                                        @if($u -> auth == 0)
                                            普通用户
                                        @elseif($u -> auth == 1)
                                            管理员<i class='fa fa-fw fa-user-secret'></i>
                                        @endif
                                    </td>
                                    <td class="status">
                                        @if($u -> status == 0)
                                            禁用
                                        @elseif($u -> status == 1)
                                            启用
                                        @endif

                                    </td>
                                    <td><a href="{{url('/admin/user/edit')}}/{{$u -> id}}"><i class="fa fa-fw  fa-edit "></i></a> &nbsp; <a href="{{url('/admin/user/delete')}}/{{$u -> id}}"><i class="fa fa-fw fa-remove"></i></a></td>
                                </tr>
                                @endforeach
                                </tbody>
                            </table>
                            {!! $data ->appends($request) -> render() !!}
                        </div><!-- /.box-body -->
                    </div><!-- /.box -->
                </div><!-- /.col -->
            </div><!-- /.row -->
        </section><!-- /.content -->
    </div><!-- /.content-wrapper -->

    <script type="text/javascript">
        window.onload = function()
        {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            //禁用事件
            $('.status').on('click', function()
            {
                //获取当前对象
                var t = $(this);
                var id = $(this).parent().find('.ids').html();
                // alert(id);
                $.ajax({
                    type:"POST",
                    url:"{{ asset('admin/user/ajaxStatus') }}",
                    data:{id:id},
                    success:function(data)
                    {
                        // alert(data);
                       if(data == 2)
                       {
                            alert('修改成功');
                       }else if(data == 0)
                       {
                            t.html('禁用');
                       }else if(data == 1)
                       {
                            t.html('启用');
                       }
                    },
                    error:function()
                    {
                        alert('异常');
                    }
                });
            });
            //管理员事件
            $('.auth').on('click', function()
            {

                //获取当前对象
                var t = $(this);
                var id = $(this).parent().find('.ids').html();
                // alert(id);
                $.ajax({
                    type:"POST",
                    url:"{{asset('/admin/user/ajaxAuth')}}",
                    data:{id:id},
                    success:function(data)
                    {
                        if(data == 1)
                        {
                            t.html("管理员<i class='fa fa-fw fa-user-secret'></i>");
                        }else if(data == 2)
                        {
                            t.html('普通用户');
                        }else if(data == 0)
                        {
                            alert('修改成功'); 
                        }
                    },
                    error:function()
                    {
                        alert('异常');
                    }

                });
            });

            
        }
    </script>
    


@endsection